package com.boco.blch.analysiser.compliance.HW.manager;

import com.boco.blch.analysiser.compliance.ComplianceResolved;
import com.boco.blch.analysiser.util.XmlUtil;

/**
 * YARN&MR租户之间的资源隔离安全基线要求项
 * 【控制节点路径】：
 * 1、查看/opt/huawei/bigdata/etc/2_21_ResourceManager/capacity-scheduler.xml 的配置项yarn.scheduler.capacity.root.queues的值，
 * 如：QueueA,QueueB   --以逗号分隔；
 * 3、分别以 第二步的队列，做如下确认
 * yarn.scheduler.capacity.root.QueueA.capacity的值不为0，且小于100
 * yarn.scheduler.capacity.root.QueueA.maximum-capacity的值不为空或不为-1；
 * yarn.scheduler.capacity.root.QueueA.minimum-user-limit-percent的值不为空或不为100；
 * yarn.scheduler.capacity.root.QueueA.user-limit-factor的值为空或为1；
 * yarn.scheduler.capacity.QueueA.maximum-am-resource-percent的值不为0且小于1；
 * @author admin
 *
 */
public class BOCO_Yarn_MultiTenancySecurity_ResourceInsulate implements ComplianceResolved {
	
	private final static String capacity_name = "yarn.scheduler.capacity.root.queues";

	@Override
	public int realEchoResolved(String echoResult) {
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			if(result == FAILED && echoResult.indexOf("ResourceManager-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("ResourceManager-start")+"ResourceManager-start".length(), echoResult.indexOf("ResourceManager-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("JobHistoryServer-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("JobHistoryServer-start")+"JobHistoryServer-start".length(), echoResult.indexOf("JobHistoryServer-end")).trim();
				result = getResult(strResult);
			}
		}
		return result;
	}
	
	/**
	 * 【控制节点路径】：
	 * 1、查看/opt/huawei/bigdata/etc/2_21_ResourceManager/capacity-scheduler.xml 的配置项yarn.scheduler.capacity.root.queues的值，
	 * 如：QueueA,QueueB   --以逗号分隔；
	 * 3、分别以 第二步的队列，做如下确认
	 * yarn.scheduler.capacity.root.QueueA.capacity的值不为0，且小于100
	 * yarn.scheduler.capacity.root.QueueA.maximum-capacity的值不为空或不为-1；
	 * yarn.scheduler.capacity.root.QueueA.minimum-user-limit-percent的值不为空或不为100；
	 * yarn.scheduler.capacity.root.QueueA.user-limit-factor的值为空或为1；
	 * yarn.scheduler.capacity.QueueA.maximum-am-resource-percent的值不为0且小于1；
	 * @param echoResult
	 * @return
	 */
	private int getResult(String echoResult){
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			String value = XmlUtil.getValueByName(echoResult, capacity_name).trim();//QueueA,QueueB
			//System.out.println("value------"+value);
			if(value != null && value.length() > 0){
				if(value.indexOf(",") != -1){
					String []strValue = value.split(",");
					for (int i = 0; i < strValue.length; i++) {
						result = checkResult(echoResult, strValue[i].trim());
						if(result == FAILED){
							break;
						}
					}
				}else{
					result = checkResult(echoResult, value);
				}
			}else{//capacity-scheduler.xml相关配置项没有值
				result = FAILED;
			}
		}
		return result;
	}
	
	private int checkResult(String echoResult,String strValue){
		//System.out.println("echoResult-----------:"+echoResult);
		//System.out.println("strValue-------:"+strValue);
		int result = FAILED;
		if(strValue != null && strValue.length() >0){
			/*
			 * yarn.scheduler.capacity.root.QueueA.capacity的值不为0，且小于100
			 * yarn.scheduler.capacity.root.QueueA.maximum-capacity的值不为空或不为-1；
			 * yarn.scheduler.capacity.root.QueueA.minimum-user-limit-percent的值不为空或不为100；
			 * yarn.scheduler.capacity.root.QueueA.user-limit-factor的值为空或为1；
			 * yarn.scheduler.capacity.QueueA.maximum-am-resource-percent的值不为0且小于1；*/
			String name_A = "yarn.scheduler.capacity.root."+strValue+".capacity";//不为0，且小于100
			String name_B = "yarn.scheduler.capacity.root."+strValue+".maximum-capacity";//值不为空或不为-1
			String name_C = "yarn.scheduler.capacity.root."+strValue+".minimum-user-limit-percent";//值不为空或不为100
			String name_D = "yarn.scheduler.capacity.root."+strValue+".user-limit-factor";//值为空或为1
			String name_E = "yarn.scheduler.capacity."+strValue+".maximum-am-resource-percent";//值不为0且小于1
			
			String value_A = XmlUtil.getValueByName(echoResult, name_A).trim();
			String value_B = XmlUtil.getValueByName(echoResult, name_B).trim();
			String value_C = XmlUtil.getValueByName(echoResult, name_C).trim();
			String value_D = XmlUtil.getValueByName(echoResult, name_D).trim();
			String value_E = XmlUtil.getValueByName(echoResult, name_E).trim();
			//不为0，且小于100
			if(value_A != null && value_A.length() > 0){
				float A = Float.parseFloat(value_A);
				if(A != 0 && A < 100){
					result = SUCCESS;
				}else{
					result = FAILED;
				}
			}
			//值不为空或不为-1
			if(result == SUCCESS && value_B != null && value_B.length() > 0 && !value_B.equals("-1")){
				result = SUCCESS;
			}else{
				result = FAILED;
			}
			//值不为空或不为100
			if(result == SUCCESS && value_C != null && value_C.length() > 0 && !value_C.equals("100")){
				result = SUCCESS;
			}else{
				result = FAILED;
			}
			//值为空或为1
			if(result == SUCCESS && (value_D == null || value_D.length() <= 0 || value_D.equals("1"))){
				result = SUCCESS;
			}else{
				result = FAILED;
			}
			
			//不为0，且小于1
			if(result == SUCCESS && value_E != null && value_E.length() > 0){
				float E = Float.parseFloat(value_E);
				if(E != 0 && E < 1){
					result = SUCCESS;
				}else{
					result = FAILED;
				}
			}
		}
		return result;
	}
	
	


}
